<#escape x as (x)!"">
	<#if columns??>
		<#list columns as column>
		<#if column.map??>
    var ${column.attrname}Map = {}
		</#if>
		</#list>
	</#if>

$(function () {
    $("#jqGrid").jqGrid({
        url: '../${pathName}/list',
        datatype: "json",
        colModel: [			
	<#if columns??> 
		<#list columns as column>
		    <#if !column.nolist>
                <#if column.isPkey>
                    { label: '${column.shortComments}', name: '${column.attrname}', index: '${column.columnName}', width: 50, key: true },
                <#else>
                    <#if column.map??>
                    { label: '${column.shortComments}', name: '${column.attrname}', index: '${column.columnName}', width:80 ,
                    formatter: function(value, options, row){return ${column.attrname}Map[value]; }
                    }<#if column_has_next>,</#if>
                    <#else>
                    { label: '${column.shortComments}', name: '${column.attrname}', index: '${column.columnName}',
                    width: 80 },
                    </#if>
                </#if>
			</#if>
		</#list>
	</#if>
	                { label: '操作', sortable: false, name: '${pk.attrname}', width: 50, formatter: function(value,
	                options, row){
    						return '<button onclick="openDetail(\''+value+'\')" class="btn btn-info btn-sm">查看</button>';
    				}}
        ],
		viewrecords: true,
		height: "100%",
        rowNum: 30,
		rowList : [30,50,100],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "data.list",
            page: "data.currPage",
            total: "data.totalPage",
            records: "data.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var pkid;
function openDetail(value){
    pkid = value
    $(viewbtn).click()
}

var vm = new Vue({
	el:'#rrapp',
	data:{
	    q:{
            searchName: null
        },
        isadd: false,
		showList: true,
		showView: false,
		title: null,
        addFlag: false,
        editFlag: false,
		${classname}: {},
        columnsList: [
          <#if columns??>
              <#list columns as column>
                <#if !column.nolist>
          {name: '${column.shortComments}', field: '${column.attrname}' , isShow: true},
                </#if>
              </#list>
          </#if>
        ],
	<#if columns??>
		<#list columns as column>
		<#if column.map??>
        ${column.attrname}List: [
        <#list column.map?keys as key> 
          {'value': ${key}, 'text': '${column.map[key]}'}<#if key_has_next>,</#if>
        </#list>
        ],
        ${column.attrname}Map: {},
        ${column.attrname}SearchList: {},
		</#if>
		</#list>
	</#if>
		rules: {
	<#if columns??>
		<#list columns as column>
                    <#if !column.noinput && column.nullable=="N">
                      <#if column.inputType?? &&(column.inputType=="select" || column.inputType=="radio" || column.inputType=="date")>
          ${column.attrname}: [
            {required: true, message: '请选择${column.shortComments}', trigger: 'change'}
          ],
                      <#else>
          ${column.attrname}: [
            {required: true, message: '请输入${column.shortComments}', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字', trigger: 'blur'}
          ],
                      </#if>
                    </#if>
          </#list>
          test:[]
       </#if> 
       }
	},
    mounted: function () {
    	<#if columns??>
    		<#list columns as column>
    		<#if column.map??>
        ${column.attrname}Map = list2Map(this.${column.attrname}List)
        this.${column.attrname}Map = list2Map(this.${column.attrname}List)
    		</#if>
    		</#list>
    	</#if>

    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
		    vm.isadd = true; 
			vm.showList = false;
			vm.showView = false;
			vm.title = "新增";
			vm.${classname} = {};
		},
		update: function (event) {
			var ${pk.attrname} = getSelectedRow();
			if(${pk.attrname} == null){
				return ;
			}
			vm.showList = false;
			vm.showView = false;
            vm.title = "修改";
            vm.getInfo(${pk.attrname})
		},
		view: function(){
        	vm.title = "查看";
        	vm.getInfo(pkid);
        	vm.showList = false;
        	vm.showView = true;
        },
        changeToUpdate:function(){
            vm.showView = false;
        },
		saveOrUpadateValid:function (valid){
            if (valid) {
                var url = vm.isadd ? "../${pathName}/save" : "../${pathName}/update";
                $.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.${classname}),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
            }else{
                alert('验证未通过');
            }
        },
		saveOrUpdate: function (event) {
			this.$refs['${classname}Form'].validate(vm.saveOrUpadateValid);
		},
		del: function (event) {
			var ${pk.attrname}s = getSelectedRows();
			if(${pk.attrname}s == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../${pathName}/delete",
				    data: JSON.stringify(${pk.attrname}s),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		delOne: function (event) {

        	var ${pk.attrname}s = new Array(pkid)
        	if(${pk.attrname}s == null){
        		return ;
        	}
        	confirm('确定要删除该记录？', function(){
        		$.ajax({
        			type: "POST",
        			url: "../${pathName}/delete",
        			data: JSON.stringify(${pk.attrname}s),
        			success: function(r){
                        if(r.code == 0){
                                        alert('操作成功', function(index){
                                            $("#jqGrid").trigger("reloadGrid");
                                        });
                                        vm.showList = true;
                        }else{
                            alert(r.msg);
                        }
        			}
        		});
        	});
        },
		getInfo: function(${pk.attrname}){
			$.get("../${pathName}/info/"+${pk.attrname}, function(r){
                vm.${classname} = r.data;
            });
		},
		reload: function (event) {
			vm.isadd = false;
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
			    postData:{'searchName': vm.q.searchName},
                page:page
            }).trigger("reloadGrid");
		}
	}
});
</#escape>